package com.zjl.util;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BaseServlet
 */
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		String reqName = req.getParameter("reqName");
		Class class1 = this.getClass();
		System.out.println("你访问的是"+class1.getName()+"."+reqName+"()");
		Method method = null;
		
		try {
			method = class1.getMethod(reqName,HttpServletRequest.class,HttpServletResponse.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("您要调用的方法：" + reqName +
					 "(HttpServletRequest,HttpServletResponse)，它不存在！");
		}
		
		try {
			method.invoke(this,req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("您调用的方法：" + reqName + ",它内部抛出了异常！");
			 throw new RuntimeException(e);
		}
	}


}
